home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_2 / fixpt.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  600 b   |  25 lines  |  [MATF/MATL]

  1. function [p0,err,P] = fixpt(g,p0,tol,max1)
  2. % [p0,err] = fixpt(g,p0,tol,max1)
  3. % [p0,err,P] = fixpt(g,p0,tol,max1)
  4. % Fixed point iteration.
  5. % g is the function, input.
  6. % p0 is the starting value, input.
  7. % tol is the tolerance, input.
  8. % max1 is the maximum number of iterations, input.
  9. % p0 is the fixed point, output.
  10. % err is the error estimate for p0, output.
  11. % P  is the vector of iterations, output.
  12. P(1) = p0;
  13. err = 1;
  14. relerr = 1;
  15. p1 = p0;
  16. for k=1:max1,
  17.   p1 = feval(g,p0);
  18.   err = abs(p1-p0);
  19.   relerr = err/(abs(p1)+eps);
  20.   if (err<tol) | (relerr<tol), break; end
  21.   p0 = p1;
  22.   P(k+1) = p1;
  23. end
  24.  
  25.